const app = getApp()
const util = require("../../utils/utils.js")

Page({

  /**
   * 页面的初始数据
   */
  data: {
    loginUserInfo: null,
    userPhotos: [],
    userNicknames: [],
    friends: [
    ],
    toWho: [],
    meWho: []
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  getOwnerInfo(userId, index) {
    var that = this
    wx.request({
      url: 'http://localhost:8888/account/getAccount',
      method: 'GET',
      data: {
        id: userId
      },
      header: {
        'content-type': 'application/json'
      },
      success: function (res) {
        var iPhoto = "userPhotos["+index+"]"
        var iNickname = "userNicknames["+index+"]"
        that.setData({
          [iPhoto]: res.data.data.headimg,
          [iNickname]: res.data.data.username
        })
        //console.log(that.data);
      }
    })
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    wx.setNavigationBarTitle({
      title: "消息"
    });
    var that = this
    that.setData({
      loginUserInfo: app.globalData.userInfo
    })
    wx.request({
      url: 'http://localhost:8888/chatting/getChattingByUser',
      method: 'GET',
      data: {
        user: app.globalData.loginAccount
      },
      header: {
        'content-type': 'application/json'
      },
      success: function (res) {
        that.setData({
          friends: res.data.data
        })
        var items = that.data.friends
        for(var i=0;i<items.length;i++) {
          items[i].time = util.strToArr(items[i].time)
          items[i].time = util.timeInMsgList(items[i].time[items[i].time.length-1])
          items[i].content = util.strToArr(items[i].content)
          items[i].content = items[i].content[items[i].content.length-1]
        }
        that.setData({
          friends: items
        })
        var length = res.data.data.length
        for(var i=0;i<length;i++) {
          if(res.data.data[i].user1 == app.globalData.loginAccount) {
             that.getOwnerInfo(res.data.data[i].user2, i)
             var iWho = "toWho["+i+"]"
             var iMe = "meWho["+i+"]"
             that.setData({
               [iWho]: res.data.data[i].user2,
               [iMe]: 1
             })
          }
          else {
            that.getOwnerInfo(res.data.data[i].user1, i)
            var iWho = "toWho["+i+"]"
            var iMe = "meWho["+i+"]"
             that.setData({
               [iWho]: res.data.data[i].user1,
               [iMe]: 2
             })
          }
        }
        //console.log(that.data);
      }
    })
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },
  gotoContact(e) {
    var id = e.currentTarget.dataset.id
    wx.request({
      url: 'http://localhost:8888/chatting/iReadIt',
      method: 'POST',
      data: {
        id: id
      },
      header: {
        'content-type': 'application/x-www-form-urlencoded'
      },
      success: function (res) {
      }
    })
    app.globalData.chatWho = e.currentTarget.dataset.who
    wx.navigateTo({
      url: '../chat-detail/detail',
    })
  }
})